<template>
<!-- 筛选区 -->
   <div class="sub-filter" v-if="filterData && filterLoading === false">
      <div class="item">
       <div class="head">品牌:</div>
       <div class="body" >
         <!-- <a href="javascript:;" class="ellipsis active" :class="{active:!id}">全部</a> -->
         <a  href="javascript:;" @click="ChangeBrand(sub.id)" class="ellipsis" :title="sub.name" :class="{active:filterData.brands.selectionId === sub.id}"  v-for="sub in filterData.brands " :key="sub.id">{{sub.name}}</a>
       </div>
     </div>
     <div class="item" v-for="topname in filterData.saleProperties" :key="topname.id">
       <div class="head">{{topname.name}}:</div>
       <div class="body" >
         <!-- <a href="javascript:;" class="ellipsis" :class="{active:!id}">全部</a> -->
         <a href="javascript:;"  @click="ChangeProp(topname,item.id)"  class="ellipsis"  :title="item.name" :class="{active:topname.newid === item.id}"  v-for="item in topname.properties " :key="item.id">{{item.name}}</a>
       </div>
     </div>
   </div>

   <!-- 骨架 -->
     <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>
<script>
// import { computed } from 'vue-demi'
import { useRoute } from 'vue-router'
import { reactive, ref, watch } from 'vue'
import { findFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  emits: ['filter-change'],
  setup (props, { emit }) {
    const route = useRoute()
    const filterData = ref(null)
    const filterLoading = ref(true)
    watch(() => route.params.id, (newVal) => {
      // 变化后的id有值且处于二级类目
      if (newVal && `/category/sub/${newVal}` === route.path) {
        filterLoading.value = true
        findFilter(route.params.id).then(data => {
          filterData.value = data.result
          // 品牌名
          filterData.value.brands.selectionId = ref(null)
          filterData.value.brands.unshift({ id: null, name: '全部' })
          // 标签名
          filterData.value.saleProperties.forEach(item => {
            item.newid = ref(null)
            item.properties.unshift({ id: null, name: '全部' })
          })
          console.log(filterData.value)
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    // 获取筛选参数的函数
    const getFilterParams = () => {
      const obj = reactive({ brandId: null, attrs: [] })
      // 品牌
      obj.brandId = filterData.value.brands.selectionId
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        if (item.newid) {
          const prop = item.properties.find(prop => prop.id === item.newid)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (!obj.attrs.length) { obj.attrs = null } // null不会发请求给后台
      return obj
    }
    // 1.记录当前选择的品牌
    const ChangeBrand = (brandid) => {
      if (filterData.value.brands.selectionId === brandid) return

      filterData.value.brands.selectionId = brandid
      emit('filter-change', getFilterParams())
    }
    // 2.记录选择的销售属性
    const ChangeProp = (obj, Propid) => {
      if (obj.newid === Propid) return
      obj.newid = Propid
      emit('filter-change', getFilterParams())
    }
    return { filterData, filterLoading, ChangeBrand, ChangeProp }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          max-width: 112px;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
    .xtx-skeleton {
        padding: 10px 0;
    }
  }
</style>
